package com.lzy.crm.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.lzy.crm.base.page.PageQuery;
import com.lzy.crm.base.util.BootStrapReturn;
import com.lzy.crm.base.util.JsonUtil;
import com.lzy.crm.base.util.StringUtil;
import com.lzy.crm.domain.Dict;
import com.lzy.crm.service.DictService;
import com.lzy.crm.util.BootJsonResponse;

/**
 * 字典配置管理
 * @author csp
 *
 */
@Controller
@RequestMapping("/admin")
public class DictController {
	
	Logger logger= Logger.getLogger(DictController.class);
	
	@Resource
	DictService dictService;
	
	
	@RequestMapping("/dict")  
	public String dict(HttpServletRequest request,ModelMap model) throws Exception{
		return "param/dict/dict_list";
	}
	
	@RequestMapping(path="/dict_data",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String noticeListData(HttpServletRequest request, @RequestParam Map<String, String> form,
			ModelMap model) {
		BootStrapReturn<Dict> result = new BootStrapReturn<Dict>();
		try {
			String dictType = request.getParameter("dictType");
			String dictName = request.getParameter("dictName");
			Integer currentPage = Integer.parseInt(request.getParameter("numPerPage"));
			Integer numPerPage = Integer.parseInt(request.getParameter("pageNum"));
			PageQuery pageQuery = new PageQuery(numPerPage,currentPage);
			pageQuery.setIsQueryTotal(true); 
			List<Dict> dictList = dictService.getDictList(dictType, dictName, pageQuery);
			result.setPage(pageQuery.getPageIndex());
			result.setTotal(pageQuery.getTotalCount());
			result.setRows(dictList);
		} catch (Exception e) {
			logger.error("字典列表", e);
		}
		return JsonUtil.toJson(result) ;
	}
	
	@RequestMapping("/dict_edit")  
	public String dictEdit(HttpServletRequest request,ModelMap model) throws Exception{
		String id= request.getParameter("id");
		if(StringUtil.isNotEmpty(id)){
			Dict dict = dictService.getById(id);
			model.put("dict", dict);
		}
		return "param/dict/dict_edit";
	}

	
	@RequestMapping(path="/dict_edit_save",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String dictEditSave(HttpServletRequest request,ModelMap model) {
		BootJsonResponse bootJsonResponse =new BootJsonResponse();
		try {
			String id= request.getParameter("id");
			String dictType = request.getParameter("dictType");
			String dictName= request.getParameter("dictName");
			String dictValue = request.getParameter("dictValue");
			String orderSort = request.getParameter("orderSort");
			String isDel =request.getParameter("isDel");
			String remark =request.getParameter("remark");
			if(StringUtil.isNotEmpty(id)){
				Dict dict=dictService.getById(id);
				if(StringUtil.isNotEmpty(dictName)){
					dict.setDictName(dictName);
				}
				if(StringUtil.isNotEmpty(dictType)){
					dict.setDictType(dictType);
				}
				if(StringUtil.isNotEmpty(dictValue)){
					dict.setDictValue(dictValue);
				}
				if(StringUtil.isNotEmpty(remark)){
					dict.setRemark(remark);
				}
				if(StringUtil.isNotEmpty(orderSort)){
					dict.setOrderSort(Integer.parseInt(orderSort));
				}
				dict.setUpdateTime(new Date());
				dictService.updateByPrimaryKeySelective(dict);
			}else{
				Dict dict=new Dict();
				dict.setDictName(dictName);
				dict.setDictType(dictType);
				dict.setDictValue(dictValue);
				dict.setIsDel(isDel);
				dict.setRemark(remark);
				if(StringUtil.isNotEmpty(orderSort)){
					dict.setOrderSort(Integer.parseInt(orderSort));
				}
				dict.setCreateTime(new Date());
				dictService.insertSelective(dict);
			}
			bootJsonResponse.setMessage("操作成功");
			bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_SUCCESS);
		} catch (Exception e) {
			bootJsonResponse.setMessage("操作失败，请联系管理员");
			bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_FAILURE);
			logger.error("修改个字典信息失败", e);
		}
		return JsonUtil.toJson(bootJsonResponse);
	}
	
	
	@RequestMapping(path="/dict_del",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String dictDelSave(HttpServletRequest request,ModelMap model) {
		BootJsonResponse bootJsonResponse =new BootJsonResponse();
		try {
			String id= request.getParameter("id");
			dictService.deleteByPrimaryKey(id);
			bootJsonResponse.setMessage("操作成功");
			bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_SUCCESS);
		} catch (Exception e) {
			bootJsonResponse.setMessage("操作失败，请联系管理员");
			bootJsonResponse.setStatusCode(BootJsonResponse.STATUS_CODE_FAILURE);
			logger.error("修改个字典信息失败", e);
		}
		return JsonUtil.toJson(bootJsonResponse);
	}
	
}
